//! Copyright 2012 Xicut Technology, Inc.
//! All rights reserved
//!
#include "BoardHardware.h"
#include "Config.h"
#include "Sensor.h"

#if defined (__FW_BUILD__)
extern "C"  {
#include "stm32f10x.h"
#include "Config.h"
}
#endif

cSensorCheck cSensor::sSensorStatus[SS_MAX];


void cSensor::InitReset()
{
#if defined (__FW_BUILD__)
    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_APB2PeriphClockCmd(SW0_PERIPH, ENABLE);
    GPIO_InitStructure.GPIO_Pin = SW0_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SW0_PORT, &GPIO_InitStructure);

    RCC_APB2PeriphClockCmd(SW1_PERIPH, ENABLE);
    GPIO_InitStructure.GPIO_Pin = SW1_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SW1_PORT, &GPIO_InitStructure);

    RCC_APB2PeriphClockCmd(SW2_PERIPH, ENABLE);
    GPIO_InitStructure.GPIO_Pin = SW2_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SW2_PORT, &GPIO_InitStructure);

    RCC_APB2PeriphClockCmd(SW3_PERIPH, ENABLE);
    GPIO_InitStructure.GPIO_Pin = SW3_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SW3_PORT, &GPIO_InitStructure);

    RCC_APB2PeriphClockCmd(SW4_PERIPH, ENABLE);
    GPIO_InitStructure.GPIO_Pin = SW4_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SW4_PORT, &GPIO_InitStructure);

    RCC_APB2PeriphClockCmd(SW5_PERIPH, ENABLE);
    GPIO_InitStructure.GPIO_Pin = SW5_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SW5_PORT, &GPIO_InitStructure);

    RCC_APB2PeriphClockCmd(SW6_PERIPH, ENABLE);
    GPIO_InitStructure.GPIO_Pin = SW6_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SW6_PORT, &GPIO_InitStructure);

    RCC_APB2PeriphClockCmd(SW7_PERIPH, ENABLE);
    GPIO_InitStructure.GPIO_Pin = SW7_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SW7_PORT, &GPIO_InitStructure);

    RCC_APB2PeriphClockCmd(SW8_PERIPH, ENABLE);
    GPIO_InitStructure.GPIO_Pin = SW8_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SW8_PORT, &GPIO_InitStructure);

    RCC_APB2PeriphClockCmd(SW9_PERIPH, ENABLE);
    GPIO_InitStructure.GPIO_Pin = SW9_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SW9_PORT, &GPIO_InitStructure);
#endif
    sSensorStatus[SS_CONVEYER_1].SetTimeDebounce(cConfig::SS_CONVEYER_1_DEBOUNCE);
    sSensorStatus[SS_CONVEYER_2].SetTimeDebounce(cConfig::SS_CONVEYER_2_DEBOUNCE);
    sSensorStatus[SS_CONVEYER_3].SetTimeDebounce(cConfig::SS_CONVEYER_3_DEBOUNCE);
    sSensorStatus[SS_CARBIN_1].SetTimeDebounce(cConfig::SS_CARBIN_1_DEBOUNCE);
    sSensorStatus[SS_CARBIN_2].SetTimeDebounce(cConfig::SS_CARBIN_2_DEBOUNCE);
    sSensorStatus[SS_CARBIN_3].SetTimeDebounce(cConfig::SS_CARBIN_3_DEBOUNCE);
    sSensorStatus[SS_TOP_CARBIN].SetTimeDebounce(cConfig::SS_TOP_CARBIN_DEBOUNCE);
    sSensorStatus[SS_GATE].SetTimeDebounce(cConfig::SS_GATE_DEBOUNCE);
    sSensorStatus[SS_CARBIN].SetTimeDebounce(cConfig::SS_CARBIN_DEBOUNCE);
    sSensorStatus[SS_PRODUCT_OUT].SetTimeDebounce(cConfig::SS_PRODUCT_OUT_DEBOUNCE);

    sSensorStatus[SS_CONVEYER_1].InitReset((SS_CONVEYER_1_ACTIVE == SS_CONVEYER_1_PIN));
    sSensorStatus[SS_CONVEYER_2].InitReset((SS_CONVEYER_2_ACTIVE == SS_CONVEYER_2_PIN));
    sSensorStatus[SS_CONVEYER_3].InitReset((SS_CONVEYER_3_ACTIVE == SS_CONVEYER_3_PIN));
    sSensorStatus[SS_CARBIN_1].InitReset((SS_CARBIN_1_ACTIVE == SS_CARBIN_1_PIN));
    sSensorStatus[SS_CARBIN_2].InitReset((SS_CARBIN_2_ACTIVE == SS_CARBIN_2_PIN));
    sSensorStatus[SS_CARBIN_3].InitReset((SS_CARBIN_3_ACTIVE == SS_CARBIN_3_PIN));
    sSensorStatus[SS_TOP_CARBIN].InitReset((SS_TOP_CARBIN_ACTIVE == SS_TOP_CARBIN_PIN));
    sSensorStatus[SS_GATE].InitReset((SS_GATE_ACTIVE == SS_GATE_PIN));
    sSensorStatus[SS_CARBIN].InitReset((SS_CARBIN_ACTIVE == SS_CARBIN_PIN));
    sSensorStatus[SS_PRODUCT_OUT].InitReset((SS_PRODUCT_OUT_ACTIVE == SS_PRODUCT_OUT_PIN));
}


bool cSensor::GetStatus(eSensorID ss)
{
    return (sSensorStatus[ss].Pin);
}


bool cSensor::GetChange(eSensorID ss)
{
    bool temp = (sSensorStatus[ss].PinChange);
    sSensorStatus[ss].PinChange = false;
    return (temp);
}


void cSensor::RunOne()
{
    sSensorStatus[SS_CONVEYER_1].CheckChange((SS_CONVEYER_1_ACTIVE == SS_CONVEYER_1_PIN));
    sSensorStatus[SS_CONVEYER_2].CheckChange((SS_CONVEYER_2_ACTIVE == SS_CONVEYER_2_PIN));
    sSensorStatus[SS_CONVEYER_3].CheckChange((SS_CONVEYER_3_ACTIVE == SS_CONVEYER_3_PIN));
    sSensorStatus[SS_CARBIN_1].CheckChange((SS_CARBIN_1_ACTIVE == SS_CARBIN_1_PIN));
    sSensorStatus[SS_CARBIN_2].CheckChange((SS_CARBIN_2_ACTIVE == SS_CARBIN_2_PIN));
    sSensorStatus[SS_CARBIN_3].CheckChange((SS_CARBIN_3_ACTIVE == SS_CARBIN_3_PIN));
    sSensorStatus[SS_TOP_CARBIN].CheckChange((SS_TOP_CARBIN_ACTIVE == SS_TOP_CARBIN_PIN));
    sSensorStatus[SS_GATE].CheckChange((SS_GATE_ACTIVE == SS_GATE_PIN));
    sSensorStatus[SS_CARBIN].CheckChange((SS_CARBIN_ACTIVE == SS_CARBIN_PIN));
    sSensorStatus[SS_PRODUCT_OUT].CheckChange((SS_PRODUCT_OUT_ACTIVE == SS_PRODUCT_OUT_PIN));
}

